package com.laozhao.test;


import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author wangchong
 */
public class TabPanel extends JPanel{
    private JLabel title;
    private CloseButton closebutton;
    private final JTabbedPane pane;

    public TabPanel(String s,JTabbedPane pane){
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        title=new JLabel(s);
        this.pane=pane;
        closebutton=new CloseButton();
        add(title);
        add(closebutton);
        title.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        setOpaque(false);
    }

    private class CloseButton extends JButton {
        private ImageIcon icon;
        public CloseButton(){

    		ImageIcon closeXIcon = new ImageIcon("D:/close.gif");
//            icon=new ImageIcon(getClass().getResource("/Image/Button/close.png"));
//            setSize(closeXIcon.getImage().getWidth(null),icon.getImage().getHeight(null));
            setIcon(closeXIcon);
            setBorder(null);
            setBorderPainted(false);
            setFocusPainted(false);
//            setPressedIcon(new ImageIcon(getClass().getResource("/Image/Button/close_pressed.png")));
//            setRolloverIcon(new ImageIcon(getClass().getResource("/Image/Button/close_rollover.png")));
            addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    pane.remove(pane.indexOfTabComponent(TabPanel.this));
                    if (pane.getTabCount()==0) {
                        pane.addTab("blank", null);
                    }
                }
            });
        }
    }

}

